<template>
    <div class="ve_container">
        <!-- 搜索 -->
        <el-form ref="queryForm" :inline="true" :model="params">
            <el-form-item label="原仓库ID" prop="sourceRepositoryId">
                <el-select
                    v-model="sourceRepositoryId"
                    placeholder="原仓库ID"
                    clearable
                    @change="onSubmit(params, getDataList)"
                >
                    <el-option
                        v-for="item in repositoryList"
                        :key="item.id"
                        :label="item.label"
                        :value="item.id"
                        :namespace="item"
                    />
                </el-select>
            </el-form-item>

            <el-form-item label="目标仓库ID" prop="tagRepositoryId">
                <el-select
                    v-model="tagRepositoryId"
                    placeholder="目标仓库ID"
                    clearable
                    @change="onSubmit(params, getDataList)"
                >
                    <el-option
                        v-for="item in repositoryList"
                        :key="item.id"
                        :label="item.label"
                        :value="item.id"
                        :namespace="item"
                    />
                </el-select>
            </el-form-item>

            <el-form-item>
                <el-button
                    type="primary"
                    @click="onSubmit(params, getDataList)"
                >
                    {{ buttons.search.name }}
                </el-button>
                <el-button @click="resetForm(queryForm, params, getDataList)">
                    重置
                </el-button>
            </el-form-item>
        </el-form>

        <!-- 列表-->
        <ve-table
            :table="{
                data: tableData,
            }"
            :pagination="{
                onSizeChange: (val) =>
                    handleSizeChange(val, params, getDataList),
                onCurrentChange: (val) =>
                    handleCurrentChange(val, params, getDataList),
                currentPage: current,
                pageSize: size,
                total: total,
            }"
        >
            <template #tool_bar>
                <el-button
                    v-permission="['backup']"
                    size="small"
                    type="primary"
                    @click="handleEdit(buttons.backup.name)"
                >
                    {{ buttons.backup.name }}
                </el-button>

                <el-button
                    v-permission="['downLoadExcel']"
                    size="small"
                    type="primary"
                    @click="downLoadExcel()"
                >
                    {{ buttons.downLoadExcel.name }}
                </el-button>
                <el-button
                    v-permission="['downLoadExportTemplateExcel']"
                    size="small"
                    type="primary"
                    @click="downLoadExportTemplateExcel()"
                >
                    {{ buttons.downLoadExportTemplateExcel.name }}
                </el-button>
                <el-button
                    v-permission="['importExcel']"
                    size="small"
                    type="primary"
                    @click="handleUpload(buttons.importExcel.name)"
                >
                    {{ buttons.importExcel.name }}
                </el-button>
            </template>

            <el-table-column
                prop="id"
                label="备份ID"
                show-overflow-tooltip
            ></el-table-column>

            <el-table-column
                prop="sourceRepositoryId"
                label="原仓库ID"
                show-overflow-tooltip
            ></el-table-column>

            <el-table-column
                prop="tagRepositoryId"
                label="目标仓库ID"
                show-overflow-tooltip
            ></el-table-column>

            <el-table-column
                prop="totalImageNum"
                label="总数量"
            ></el-table-column>

            <el-table-column
                prop="backupImageNum"
                label="备份数量"
            ></el-table-column>
            <el-table-column
                prop="createTime"
                label="创建时间"
            ></el-table-column>

            <el-table-column fixed="right" label="操作">
                <template v-slot:default="{ row }">
                    <el-button
                        v-permission="['del']"
                        @click.prevent="handleDel(row.id)"
                        type="danger"
                        size="small"
                    >
                        {{ buttons.del.name }}
                    </el-button>
                    <el-button
                        v-permission="['record']"
                        @click.prevent="handleRecord(row)"
                        type="primary"
                        size="small"
                    >
                        {{ buttons.record.name }}
                    </el-button>
                </template>
            </el-table-column>
        </ve-table>

        <!-- 编辑组件 -->
        <DockerRepositoryBackupEdit
            v-if="showDialog"
            :rowData="rowData"
            :title="dialogTitle"
            :showDialog="showDialog"
            @closeDialog="closeEditDialog($event)"
        />
        <!-- 上传组件 -->
        <DockerRepositoryBackupUpload
            v-if="showUploadDialog"
            :title="dialogTitle"
            :showDialog="showUploadDialog"
            @closeDialog="closeUploadDialog($event)"
        />
        <!-- 详情组件 -->
        <DockerRepositoryBackupRecordInfo
            v-if="showRecordDialog"
            :rowData="rowData"
            :showDialog="showRecordDialog"
            @closeDialog="closeRecordDialog($event)"
        />
    </div>
</template>
<script>
import menu from "./components/menu";
import { moduleName } from "@/config";

export default {
    data: () => ({
        description: "仓库备份",
        buttons: {
            search: { name: "查询" },
            backup: { name: "备份" },
            del: { name: "删除" },
            record: { name: "备份记录" },
            downLoadExcel: { name: "导出结果" },
            downLoadExportTemplateExcel: { name: "导出模版" },
            importExcel: { name: "导入" },
        },
        // type 0:目录 1：菜单 2：按钮
        type: "1",
        icon: "Stamp",
        name: "仓库备份",
        parentMenu: menu,
        module: moduleName,
    }),
};
</script>

<script setup>
import DockerRepositoryBackupEdit from "./components/DockerRepositoryBackupEdit.vue";
import DockerRepositoryBackupUpload from "./components/DockerRepositoryBackupUpload.vue";
import { reactive, toRefs, ref, onMounted, getCurrentInstance } from "vue";

//*导入公共查询方法
import {
    onSubmit,
    resetForm,
    handleSizeChange,
    handleCurrentChange,
} from "@/views/pages/common";
import DockerRepositoryBackupRecordInfo from "@/views/pages/builder/components/DockerRepositoryBackupRecordInfo.vue";

const { proxy } = getCurrentInstance();

const rowData = ref(null);
const dialogTitle = ref("");
const showDialog = ref(false);
const showUploadDialog = ref(false);

const showRecordDialog = ref(false);

const queryForm = ref(null);
const tableData = ref([]);
const repositoryList = ref(null);
const params = reactive({
    sourceRepositoryId: "",
    tagRepositoryId: "",
    size: 10,
    current: 1,
    total: 0,
});
const { sourceRepositoryId, tagRepositoryId, size, current, total } =
    toRefs(params);

/**
 * @description:添加or编辑事件
 * @return {*}
 * @param title
 * @param row
 */
const handleEdit = (title, row = null) => {
    showDialog.value = true;
    dialogTitle.value = title;
    rowData.value = row;
};
/**
 * @description: dialog事件
 * @return {*}
 * @param e
 */
const closeEditDialog = (e) => {
    showDialog.value = e;
    getDataList();
};

/**
 * @description: 上传Excel文件
 * @return {*}
 * @param title
 * @param row
 */
const handleUpload = (title, row = null) => {
    showUploadDialog.value = true;
    dialogTitle.value = title;
    rowData.value = row;
};
/**
 * @description: dialog事件
 * @return {*}
 * @param e
 */
const closeUploadDialog = (e) => {
    showUploadDialog.value = e;
    getDataList();
};
/**删除行数据
 * @description:
 * @return {*}
 * @param id
 */
const handleDel = (id) => {
    proxy
        .$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "error",
        })
        .then(async () => {
            const { code } =
                await VE_API.DockerRepositoryBackupApi.dockerRepositoryBackupRemove(
                    {
                        id,
                    },
                );
            if (code === 0) {
                getDataList();
            }
        })
        .catch(() => {
            proxy.$message({
                type: "info",
                message: "已取消删除",
            });
        });
};

/** 打开备份记录
 * @description:
 * @return {*}
 * @param row
 */
const handleRecord = (row = null) => {
    showRecordDialog.value = true;
    rowData.value = row;
};
/**
 * @description: dialog事件
 * @return {*}
 * @param e
 */
const closeRecordDialog = (e) => {
    showRecordDialog.value = e;
    getDataList();
};
/**
 * 文件模版下载
 */
const downLoadExportTemplateExcel = async () => {
    queryForm.value.validate(async (valid) => {
        if (valid) {
            let res =
                await VE_API.DockerRepositoryBackupApi.dockerRepositoryBackupExportTemplateExcel(
                    params,
                    {
                        responseType: "blob",
                    },
                );
            let fileName = res.headers["file-name"];
            // 获取文件名
            fileName = decodeURIComponent(fileName);
            let url = window.URL.createObjectURL(new Blob([res.data]));
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            // eslint-disable-next-line no-undef
            link.setAttribute("download", fileName); //指定下载后的文件名，防跳转
            document.body.appendChild(link);
            link.click();
            // 释放内存
            window.URL.revokeObjectURL(link.href);
        } else {
            return false;
        }
    });
};

/**
 * 文件下载
 */
const downLoadExcel = async () => {
    queryForm.value.validate(async (valid) => {
        if (valid) {
            let res =
                await VE_API.DockerRepositoryBackupApi.dockerRepositoryBackupExportExcel(
                    params,
                    {
                        responseType: "blob",
                    },
                );
            let fileName = res.headers["file-name"];
            // 获取文件名
            fileName = decodeURIComponent(fileName);
            let url = window.URL.createObjectURL(new Blob([res.data]));
            let link = document.createElement("a");
            link.style.display = "none";
            link.href = url;
            // eslint-disable-next-line no-undef
            link.setAttribute("download", fileName); //指定下载后的文件名，防跳转
            document.body.appendChild(link);
            link.click();
            // 释放内存
            window.URL.revokeObjectURL(link.href);
        } else {
            return false;
        }
    });
};
/**
 * @description: 获取列表数据
 * @param {*}
 * @return {*}
 */
const getDataList = async () => {
    const { code, data } =
        await VE_API.DockerRepositoryBackupApi.dockerRepositoryBackupFindPage(
            params,
        );
    if (code === 0) {
        const { size, current, total, records } = data;
        params.size = size;
        params.current = current;
        params.total = total;
        tableData.value = records;
    }
};

/**
 * @description: 获取列表数据
 * @param {*}
 * @return {*}
 */
const getRepositoryDataList = async () => {
    VE_API.builder.dockerRepositoryFindList({}).then((res) => {
        if (res.code === 0) {
            if (res.data) {
                repositoryList.value = res.data ? res.data : [];
            }
        }
    });
};

onMounted(async () => {
    await getDataList();
    await getRepositoryDataList();
});
</script>

<style lang="scss" scoped></style>
